package com.phospherus.common.service.impl;

import com.phospherus.common.AttachedFile;
import com.phospherus.common.conf.UpLoadConf;
import com.phospherus.common.repository.AttachedFileRepository;
import com.phospherus.common.service.IAttachedFileService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/12/21
 */
@Service
public class AttachedFileServiceImpl extends BaseServiceImpl<AttachedFile, Long, AttachedFileRepository> implements IAttachedFileService {
    @Autowired
    private UpLoadConf upLoadConf;

    @Override
    public <S extends AttachedFile> Iterable<S> save(Iterable<S> entities) {
        return super.save(entities);
    }


    @Override
    public void addCustomerAttachedFile(String attachedFileId, Long customerId) {
        String[] fIds = attachedFileId.split(",");
        for (String fileId : fIds) {
            try {
                long id = Long.parseLong(fileId);
                AttachedFile attachedFile = findOne(id);
                if (attachedFile == null) {
                    continue;
                }
                String fileName = attachedFile.getFileName();
                File srcFile = new File(upLoadConf.getBaseDirectory() + File.separator + fileName);
                if (!srcFile.exists() || srcFile.isDirectory()) {
                    continue;
                }
                String destFileName = "attached" + File.separator + fileName.substring(fileName.lastIndexOf(File.separator) + 1);
                File destFile = new File(upLoadConf.getBaseDirectory() + File.separator + destFileName);
                if (!destFile.exists()) {
                    FileUtils.moveFile(srcFile, destFile);
                }
                attachedFile.setType(1);
                attachedFile.setCustomerId(customerId);
                attachedFile.setFileName(destFileName);
                attachedFile.setStatus(1);
                save(attachedFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public void addFormInstAttachedFile(String attachedFileId, Long formInstId, String tabName, Long tabId) {
        String[] fIds = attachedFileId.split(",");
        for (String fileId : fIds) {
            try {
                long id = Long.parseLong(fileId);
                AttachedFile attachedFile = findOne(id);
                if (attachedFile == null) {
                    continue;
                }
                String fileName = attachedFile.getFileName();
                File srcFile = new File(upLoadConf.getBaseDirectory() + File.separator + fileName);
                if (!srcFile.exists() || srcFile.isDirectory()) {
                    continue;
                }
                String destFileName = "attached" + File.separator + fileName.substring(fileName.lastIndexOf(File.separator) + 1);
                File destFile = new File(upLoadConf.getBaseDirectory() + File.separator + destFileName);
                if (!destFile.exists()) {
                    FileUtils.moveFile(srcFile, destFile);
                }
                attachedFile.setType(2);
                attachedFile.setFormInstId(formInstId);
                attachedFile.setTabName(tabName);
                attachedFile.setTabId(tabId);
                attachedFile.setFileName(destFileName);
                attachedFile.setStatus(1);
                save(attachedFile);
            } catch (IOException | NumberFormatException e) {
                continue;
            }
        }
    }

    @Override
    public AttachedFile addNewAttacheFile(String fileName, String realFileName) {
        AttachedFile attachedFile = new AttachedFile();
        attachedFile.setStatus(0);
        attachedFile.setFileName(fileName);
        attachedFile.setRealFileName(realFileName);
        save(attachedFile);
        return attachedFile;
    }

    @Override
    public List<AttachedFile> getFormAttachedFiles(Long formInstId, Long tabId,String tabName) {
        return repository.getFormAttachedFiles(formInstId, tabId,tabName);
    }
}
